﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Mes_app_control
{
    class MillWrapper : IDisposable
    {
        private SocketWrapper socketWrapper = new SocketWrapper();
        bool connected = false;
        private string lastStrError;
        public int token = 0x12345678;

        public MillWrapper()
        {
        }

        // 添加公共只读属性供外部查询连接状态
        public bool IsConnected
        {
            get { return connected; }
        }

        public string LastError
        {
            get
            {
                return lastStrError;
            }
        }
        public bool Connect(string ip, int port, int token)
        {
            try
            {
                if (!connected)
                {
                    this.token = token;
                    socketWrapper.IP = ip;
                    socketWrapper.Port = port;
                    bool res = socketWrapper.Connect();
                    this.connected = res;
                    return res;
                }
            }
            catch (Exception e)
            {
                lastStrError = e.Message;
            }
            return false;
        }

        public bool DisConnect()
        {
            try
            {
                if (this.connected)
                {
                    this.socketWrapper.Disconnect();
                    this.connected = false;
                    return true;
                }
            }
            catch (Exception e)
            {
                lastStrError = e.Message;
            }
            return false;
        }
        //获取机床当前状态。sts:0待机，1运行，2暂停，4报警
        public bool GetSts(ref int sts)
        {
            bool res = false;
            try
            {
                List<byte> values = new List<byte>(255);
                values.AddRange(new Byte[] { 0, 0 });
                values.AddRange(BitConverter.GetBytes(token));
                values.AddRange(new Byte[] { 0x6 });
                int len = 2+2;
                values.AddRange(BitConverter.GetBytes(len));
                ushort addr = 0x8000;
                values.AddRange(BitConverter.GetBytes(addr));
                ushort crc = 0x0;
                values.AddRange(BitConverter.GetBytes(crc));
                this.socketWrapper.Write(values.ToArray());
                byte[] response = this.socketWrapper.Read(1024);
                if (response.Length == 13)
                {
                    byte respres = response[3];
                    if (respres == 0)
                    {
                        sts = response[10];
                        return true;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                lastStrError = e.Message;
                res = false;
            }
            return res;
        }
        //获取R变量值，index必须在0-599内
        public bool GetRVal(int index, ref float val)
        {
            try
            {
                List<byte> values = new List<byte>(255);
                values.AddRange(new Byte[] { 0, 0 });
                values.AddRange(BitConverter.GetBytes(token));
                values.AddRange(new Byte[] { 0x5 });
                int len = 2 + 4 + 2;
                values.AddRange(BitConverter.GetBytes(len));
                short addr = 0x7000;
                values.AddRange(BitConverter.GetBytes(addr));
                values.AddRange(BitConverter.GetBytes(index));
                short crc = 0;
                values.AddRange(BitConverter.GetBytes(crc));
                this.socketWrapper.Write(values.ToArray());
                byte[] response = this.socketWrapper.Read(255);

                if (response.Length == 16)
                {
                    byte respres = response[3];
                    if (respres == 0)
                    {
                        Int32 dataLen = BitConverter.ToInt16(response, 4);
                        UInt32 paraAddr = BitConverter.ToUInt16(response, 8);
                        val = BitConverter.ToSingle(response, 10);
                        return true;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                lastStrError = e.Message;
            }
            return false;
        }
        //修改R变量值，index必须在0-599内
        public bool SetRVal(int index, float val)
        {
            try
            {
                List<byte> values = new List<byte>(255);
                values.AddRange(new Byte[] { 0, 0 });
                values.AddRange(BitConverter.GetBytes(token));
                values.AddRange(new Byte[] { 0x4 });
                int len = 2 + 4 + 4 + 2;
                values.AddRange(BitConverter.GetBytes(len));
                short addr = 0x7000;
                values.AddRange(BitConverter.GetBytes(addr));
                values.AddRange(BitConverter.GetBytes(index));
                values.AddRange(BitConverter.GetBytes(val));
                short crc = 0;
                values.AddRange(BitConverter.GetBytes(crc));
                this.socketWrapper.Write(values.ToArray());
                byte[] response = this.socketWrapper.Read(255);

                if (response.Length == 10)
                {
                    byte respres = response[3];
                    if (respres == 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception e)
            {
                lastStrError = e.Message;
            }
            return false;
        }

        public void Dispose()
        {
            socketWrapper.Dispose();
        }
    }
}
